﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Apps.IDAL;
using Apps.Models;
using System.Data;

namespace Apps.DAL.Sys
{
    public partial class SysUserRepository
    {
        public int GetUserCountByDepId(string depId)
        {
            return Context.P_Sys_GetUserCountByDepId(depId).Cast<int>().First();
        }
      
        public IQueryable<SysRole> GetRefSysRole(string id)
        {
            if (!string.IsNullOrEmpty(id))
            {
                return from m in Context.SysUser
                       from f in m.SysRole
                       where m.Id == id
                       select f;
            }
            return null;
        }

        public IQueryable<P_Sys_GetRoleByUserId_Result> GetRoleByUserId(string userId)
        {
            return Context.P_Sys_GetRoleByUserId(userId).AsQueryable();
        }
        public IQueryable<SysUser> GetListByPosId(string posId)
        {
            return Context.SysUser.Where(a => a.PosId == posId);
        }
        /// <summary>
        /// 获得部门及子部门用户  zph
        /// </summary>
        /// <param name="depId"></param>
        /// <returns></returns>
        public IQueryable<P_Sys_GetUserByDepId_Result> GetUserByDepId(string depId)
        {
            string sql = string.Format(@"select s.*,0 flag from sysuser s ,
(select * from sysstruct m start with m.id='{0}' connect by m.PARENTID=prior m.id) d
where s.depid = d.id",depId);
            return    Context.Database.SqlQuery<P_Sys_GetUserByDepId_Result>(sql).AsQueryable();
            //var query_structs = from c in Context.SysStruct
            //            where c.Id == depId
            //            select c;
            //query_structs.ToList<SysStruct>().Concat(query_structs.ToList().SelectMany(t => GetStructID(t.Id)));

            //var users = from u in Context.SysUser
            //            from s in query_structs
            //            where s.Id == u.DepId 
            //            select new P_Sys_GetUserByDepId_Result
            //            {
            //                flag = 0,
            //                Address = u.Address,
            //                Attach = u.Attach,
            //                Birthday = u.Birthday,
            //                Card = u.Card,
            //                City = u.City,
            //                CreatePerson = u.CreatePerson,
            //                CreateTime = u.CreateTime,
            //                Degree = u.Degree,
            //                DepId = u.DepId,
            //                EmailAddress = u.EmailAddress,
            //                Expertise = u.Expertise,
            //                Id = u.Id,
            //                IsReportCalendar = u.IsReportCalendar,
            //                IsSecretary = u.IsSecretary,
            //                IsSelLead = u.IsSelLead,
            //                JobState = u.JobState,
            //                JoinDate = u.JoinDate,
            //                Lead = u.Lead,
            //                LeadName = u.LeadName,
            //                Marital = u.Marital,
            //                MobileNumber = u.MobileNumber,
            //                Nationality = u.Nationality,
            //                Native = u.Native,
            //                OtherContact = u.OtherContact,
            //                Password = u.Password,
            //                PhoneNumber = u.PhoneNumber,
            //                Photo = u.Photo,
            //                Political = u.Political,
            //                PosId = u.PosId,
            //                Professional = u.Professional,
            //                Province = u.Province,
            //                QQ = u.QQ,
            //                School = u.School,
            //                Sex = u.Sex,
            //                State = u.State, 
            //                TrueName = u.TrueName,
            //                UserName = u.UserName,
            //                Village = u.Village 
            //            };
            //return users;
            //return Context.P_Sys_GetUserByDepId(depId).AsQueryable();
        }

        private IEnumerable<SysStruct> GetStructID(string pid)
        {
            var query = from c in Context.SysStruct
                        where c.ParentId  == pid
                        select c; 
            return query.ToList<SysStruct>().Concat(query.ToList().SelectMany(t => GetStructID(t.Id)));
        }

        public void UpdateSysRoleSysUser(string userId, string[] roleIds)
        {
            Context.P_Sys_DeleteSysRoleSysUserByUserId(userId);
            foreach (string roleid in roleIds)
            {
                if (!string.IsNullOrWhiteSpace(roleid))
                {
                        Context.P_Sys_UpdateSysRoleSysUser(roleid,userId);
                 }
            }
            this.SaveChanges();
        }

       public IQueryable<P_Sys_GetAllUsers_Result> GetAllUsers()
       {
           return Context.P_Sys_GetAllUsers().AsQueryable();
       }



       /// <summary>
        /// 根据ID获取一个实体
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public string GetNameById(string id)
        {
           SysUser user = this.GetById(id);
           if (user != null)
           {
               return user.TrueName;
            }
            return "";
        }

    }
}
